<html>
<head><meta charset="utf-8"><title>good example for const generics · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/good.20example.20for.20const.20generics.html">good example for const generics</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="203612256"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/good%20example%20for%20const%20generics/near/203612256" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/good.20example.20for.20const.20generics.html#203612256">(Jul 11 2020 at 20:31)</a>:</h4>
<p>For a training, I'd like to show off some in-progress Rust features, like const generics. My previous example was </p>
<div class="codehilite"><pre><span></span><code><span class="k">fn</span> <span class="nf">make_array</span><span class="o">&lt;</span><span class="k">const</span><span class="w"> </span><span class="n">N</span>: <span class="kt">usize</span><span class="o">&gt;</span><span class="p">()</span><span class="w"> </span>-&gt; <span class="p">[</span><span class="kt">i32</span><span class="p">;</span><span class="w"> </span><span class="n">N</span><span class="p">]</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="nb">Default</span>::<span class="n">default</span><span class="p">()</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>


<p>That was fine when const generics were super broken and ICEd, but now it fails because of the weirdness around <code>Default</code> and <code>[T; 0]</code>. What's another short (ideally 5 line or less) example of why such a thing is useful?</p>



<a name="203612309"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/good%20example%20for%20const%20generics/near/203612309" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/good.20example.20for.20const.20generics.html#203612309">(Jul 11 2020 at 20:32)</a>:</h4>
<p>Maybe the trait impls for arrays in libcore?</p>



<a name="203625886"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/good%20example%20for%20const%20generics/near/203625886" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/good.20example.20for.20const.20generics.html#203625886">(Jul 12 2020 at 03:43)</a>:</h4>
<p><span class="user-mention" data-user-id="116155">@Jake Goulding</span> is there even a const-generic <code>Default</code> impl for arrays? I saw a PR earlier today which seemed to add one for the first time</p>



<a name="203625933"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/good%20example%20for%20const%20generics/near/203625933" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/good.20example.20for.20const.20generics.html#203625933">(Jul 12 2020 at 03:44)</a>:</h4>
<p>this <a href="https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2018&amp;gist=3ec519f42d40fff3e8c0890d6b497ff2">works</a>:</p>
<div class="codehilite"><pre><span></span><code><span class="k">fn</span> <span class="nf">filled</span><span class="o">&lt;</span><span class="k">const</span><span class="w"> </span><span class="n">N</span>: <span class="kt">usize</span><span class="o">&gt;</span><span class="p">()</span><span class="w"> </span>-&gt; <span class="p">[</span><span class="kt">u8</span><span class="p">;</span><span class="w"> </span><span class="n">N</span><span class="p">]</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="p">[</span><span class="mh">0xff</span><span class="p">;</span><span class="w"> </span><span class="n">N</span><span class="p">]</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="203626078"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/good%20example%20for%20const%20generics/near/203626078" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/good.20example.20for.20const.20generics.html#203626078">(Jul 12 2020 at 03:49)</a>:</h4>
<p>and so does this (<a href="https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2018&amp;gist=675a1ea357f6eeb34f172169c0d7b4a2">https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2018&amp;gist=675a1ea357f6eeb34f172169c0d7b4a2</a>):</p>
<div class="codehilite"><pre><span></span><code><span class="k">fn</span> <span class="nf">collect_to_array</span><span class="o">&lt;</span><span class="n">T</span><span class="p">,</span><span class="w"> </span><span class="k">const</span><span class="w"> </span><span class="n">N</span>: <span class="kt">usize</span><span class="o">&gt;</span><span class="p">(</span><span class="k">mut</span><span class="w"> </span><span class="n">it</span>: <span class="nc">impl</span><span class="w"> </span><span class="nb">Iterator</span><span class="o">&lt;</span><span class="n">Item</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">T</span><span class="o">&gt;</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="p">[</span><span class="n">T</span><span class="p">;</span><span class="w"> </span><span class="n">N</span><span class="p">]</span><span class="w"></span>
<span class="k">where</span><span class="w"></span>
<span class="w">    </span><span class="n">T</span>: <span class="nb">Copy</span> <span class="o">+</span><span class="w"> </span><span class="nb">Default</span><span class="p">,</span><span class="w"></span>
<span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">array</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[</span><span class="n">T</span>::<span class="n">default</span><span class="p">();</span><span class="w"> </span><span class="n">N</span><span class="p">];</span><span class="w"></span>
<span class="w">    </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="n">i</span><span class="p">,</span><span class="w"> </span><span class="n">x</span><span class="p">)</span><span class="w"> </span><span class="k">in</span><span class="w"> </span><span class="n">it</span><span class="p">.</span><span class="n">by_ref</span><span class="p">().</span><span class="n">enumerate</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="n">array</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">x</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="w">    </span><span class="n">assert</span><span class="o">!</span><span class="p">(</span><span class="n">it</span><span class="p">.</span><span class="n">next</span><span class="p">().</span><span class="n">is_none</span><span class="p">());</span><span class="w"></span>
<span class="w">    </span><span class="n">array</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">fn</span> <span class="nf">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="p">[</span><span class="n">a</span><span class="p">,</span><span class="w"> </span><span class="n">b</span><span class="p">,</span><span class="w"> </span><span class="n">c</span><span class="p">,</span><span class="w"> </span><span class="n">d</span><span class="p">,</span><span class="w"> </span><span class="n">e</span><span class="p">,</span><span class="w"> </span><span class="n">f</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">collect_to_array</span><span class="p">(</span><span class="mi">0</span><span class="o">..</span><span class="mi">6</span><span class="p">);</span><span class="w"></span>
<span class="w">    </span><span class="n">dbg</span><span class="o">!</span><span class="p">(</span><span class="n">a</span><span class="p">,</span><span class="w"> </span><span class="n">b</span><span class="p">,</span><span class="w"> </span><span class="n">c</span><span class="p">,</span><span class="w"> </span><span class="n">d</span><span class="p">,</span><span class="w"> </span><span class="n">e</span><span class="p">,</span><span class="w"> </span><span class="n">f</span><span class="p">);</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="203640911"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/good%20example%20for%20const%20generics/near/203640911" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/good.20example.20for.20const.20generics.html#203640911">(Jul 12 2020 at 12:03)</a>:</h4>
<blockquote>
<p>is there even a const-generic <code>Default</code> impl for arrays?</p>
</blockquote>
<p><span class="user-mention" data-user-id="119009">@eddyb</span> no, because of the weirdness I mentioned. Right now in stable Rust, arrays implement <code>Default</code> for <code>[T; 0]</code> <strong>and</strong> <code>[T; 1, 2, 3... 32] where T: Default</code>. This makes using const genetics for them not really possible without some kind of specialization</p>



<a name="203640958"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/good%20example%20for%20const%20generics/near/203640958" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/good.20example.20for.20const.20generics.html#203640958">(Jul 12 2020 at 12:04)</a>:</h4>
<p>See <a href="https://github.com/rust-lang/rust/pull/74254">https://github.com/rust-lang/rust/pull/74254</a>, which tries to fix this</p>



<a name="203640963"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/good%20example%20for%20const%20generics/near/203640963" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/good.20example.20for.20const.20generics.html#203640963">(Jul 12 2020 at 12:04)</a>:</h4>
<p>There is still an interesting issue with trait selection of which I am actually not completely sure how easy this is fixable</p>



<a name="203640966"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/good%20example%20for%20const%20generics/near/203640966" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/good.20example.20for.20const.20generics.html#203640966">(Jul 12 2020 at 12:04)</a>:</h4>
<p>So that PR might not end up going anywhere</p>



<a name="203640972"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/good%20example%20for%20const%20generics/near/203640972" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/good.20example.20for.20const.20generics.html#203640972">(Jul 12 2020 at 12:05)</a>:</h4>
<p>But it's still a fairly clean approach imo</p>



<a name="203641264"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/good%20example%20for%20const%20generics/near/203641264" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/good.20example.20for.20const.20generics.html#203641264">(Jul 12 2020 at 12:14)</a>:</h4>
<p>Auto traits, eh.</p>



<a name="203641391"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/good%20example%20for%20const%20generics/near/203641391" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/good.20example.20for.20const.20generics.html#203641391">(Jul 12 2020 at 12:18)</a>:</h4>
<p>yeah :/ We probably shouldn't have added <code>impl&lt;T&gt; Default for [T; 0]</code> in the past, as we now require partially overlapping impls which we don't really support rn</p>



<a name="203641398"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/good%20example%20for%20const%20generics/near/203641398" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/good.20example.20for.20const.20generics.html#203641398">(Jul 12 2020 at 12:19)</a>:</h4>
<p>Yeah, but I bet whoever added that implementation felt really smart about it (and they were right!)</p>



<a name="203641447"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/good%20example%20for%20const%20generics/near/203641447" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/good.20example.20for.20const.20generics.html#203641447">(Jul 12 2020 at 12:20)</a>:</h4>
<p>It really was <span aria-label="sweat smile" class="emoji emoji-1f605" role="img" title="sweat smile">:sweat_smile:</span> so I can't even blame anyone here</p>



<a name="203641463"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/good%20example%20for%20const%20generics/near/203641463" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/good.20example.20for.20const.20generics.html#203641463">(Jul 12 2020 at 12:21)</a>:</h4>
<p>Does min specialization interact well with const genetics?</p>



<a name="203641464"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/good%20example%20for%20const%20generics/near/203641464" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/good.20example.20for.20const.20generics.html#203641464">(Jul 12 2020 at 12:22)</a>:</h4>
<p>It should be fine</p>



<a name="203641504"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/good%20example%20for%20const%20generics/near/203641504" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/good.20example.20for.20const.20generics.html#203641504">(Jul 12 2020 at 12:22)</a>:</h4>
<p>but min_specialization doesn't help here</p>



<a name="203641506"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/good%20example%20for%20const%20generics/near/203641506" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/good.20example.20for.20const.20generics.html#203641506">(Jul 12 2020 at 12:22)</a>:</h4>
<p>Oh?</p>



<a name="203641507"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/good%20example%20for%20const%20generics/near/203641507" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/good.20example.20for.20const.20generics.html#203641507">(Jul 12 2020 at 12:22)</a>:</h4>
<p>afaik we can only specialize subsets</p>



<a name="203641514"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/good%20example%20for%20const%20generics/near/203641514" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/good.20example.20for.20const.20generics.html#203641514">(Jul 12 2020 at 12:22)</a>:</h4>
<p>and we have overlapping sets</p>



<a name="203641520"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/good%20example%20for%20const%20generics/near/203641520" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/good.20example.20for.20const.20generics.html#203641520">(Jul 12 2020 at 12:23)</a>:</h4>
<p>I guess you can’t specialize to have less of a trait restriction</p>



<a name="203641522"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/good%20example%20for%20const%20generics/near/203641522" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/good.20example.20for.20const.20generics.html#203641522">(Jul 12 2020 at 12:23)</a>:</h4>
<p>yeah</p>



<a name="203641572"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/good%20example%20for%20const%20generics/near/203641572" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/good.20example.20for.20const.20generics.html#203641572">(Jul 12 2020 at 12:24)</a>:</h4>
<p>there's the "obvious" solution of changing <code>Default</code> to a lang item and manually implementing it inside of the compiler</p>



<a name="203641579"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/good%20example%20for%20const%20generics/near/203641579" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/good.20example.20for.20const.20generics.html#203641579">(Jul 12 2020 at 12:25)</a>:</h4>
<p>but nooooo</p>



<a name="203641592"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/good%20example%20for%20const%20generics/near/203641592" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/good.20example.20for.20const.20generics.html#203641592">(Jul 12 2020 at 12:26)</a>:</h4>
<p>Well we did that with <code>Clone</code> due to reasons</p>



<a name="203641653"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/good%20example%20for%20const%20generics/near/203641653" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Matthew Jasper <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/good.20example.20for.20const.20generics.html#203641653">(Jul 12 2020 at 12:27)</a>:</h4>
<p>We did it with <code>Clone</code> because we had already done it with <code>Copy</code> and it was causing ICEs.</p>



<a name="203641710"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/good%20example%20for%20const%20generics/near/203641710" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/good.20example.20for.20const.20generics.html#203641710">(Jul 12 2020 at 12:29)</a>:</h4>
<p><span class="user-mention silent" data-user-id="116118">Matthew Jasper</span> <a href="#narrow/stream/122651-general/topic/good.20example.20for.20const.20generics/near/203641653">said</a>:</p>
<blockquote>
<p>We did it with <code>Clone</code> because we had already done it with <code>Copy</code> and it was causing ICEs.</p>
</blockquote>
<p>Because we ended up with <code>T: Copy</code> but not <code>T: Clone</code>, which breaks the requirement of <code>trait Copy: Clone</code>?</p>



<a name="203641715"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/good%20example%20for%20const%20generics/near/203641715" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/good.20example.20for.20const.20generics.html#203641715">(Jul 12 2020 at 12:29)</a>:</h4>
<p>yeah</p>



<a name="203642562"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/good%20example%20for%20const%20generics/near/203642562" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/good.20example.20for.20const.20generics.html#203642562">(Jul 12 2020 at 12:53)</a>:</h4>
<p>oh god, I think I've peaked:</p>
<div class="codehilite"><pre><span></span><code><span class="sd">/// A trait implemented by all arrays which are either empty or contain a type implementing `Default`.</span>
<span class="cp">#[unstable(feature = </span><span class="s">&quot;array_default_internals&quot;</span><span class="cp">, reason = </span><span class="s">&quot;implementation detail&quot;</span><span class="cp">, issue = </span><span class="s">&quot;none&quot;</span><span class="cp">)]</span><span class="w"></span>
<span class="cp">#[marker]</span><span class="w"></span>
<span class="k">pub</span><span class="w"> </span><span class="k">trait</span><span class="w"> </span><span class="n">ArrayDefault</span><span class="w"> </span><span class="p">{}</span><span class="w"></span>

<span class="cp">#[unstable(feature = </span><span class="s">&quot;array_default_internals&quot;</span><span class="cp">, reason = </span><span class="s">&quot;implementation detail&quot;</span><span class="cp">, issue = </span><span class="s">&quot;none&quot;</span><span class="cp">)]</span><span class="w"></span>
<span class="k">impl</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="w"> </span><span class="n">ArrayDefault</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">[</span><span class="n">T</span><span class="p">;</span><span class="w"> </span><span class="mi">0</span><span class="p">]</span><span class="w"> </span><span class="p">{}</span><span class="w"></span>

<span class="cp">#[unstable(feature = </span><span class="s">&quot;array_default_internals&quot;</span><span class="cp">, reason = </span><span class="s">&quot;implementation detail&quot;</span><span class="cp">, issue = </span><span class="s">&quot;none&quot;</span><span class="cp">)]</span><span class="w"></span>
<span class="k">impl</span><span class="o">&lt;</span><span class="n">T</span>: <span class="nb">Default</span><span class="p">,</span><span class="w"> </span><span class="k">const</span><span class="w"> </span><span class="n">N</span>: <span class="kt">usize</span><span class="o">&gt;</span><span class="w"> </span><span class="n">ArrayDefault</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">[</span><span class="n">T</span><span class="p">;</span><span class="w"> </span><span class="n">N</span><span class="p">]</span><span class="w"> </span><span class="p">{}</span><span class="w"></span>

<span class="k">trait</span><span class="w"> </span><span class="n">DefaultHack</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">fn</span> <span class="nf">default_hack</span><span class="p">()</span><span class="w"> </span>-&gt; <span class="nc">Self</span><span class="p">;</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">impl</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="w"> </span><span class="n">DefaultHack</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">T</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">default</span> <span class="k">fn</span><span class="w"> </span><span class="n">default_hack</span><span class="p">()</span><span class="w"> </span>-&gt; <span class="nc">Self</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="n">unreachable</span><span class="o">!</span><span class="p">();</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">impl</span><span class="o">&lt;</span><span class="n">T</span>: <span class="nb">Default</span><span class="o">&gt;</span><span class="w"> </span><span class="n">DefaultHack</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">T</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">fn</span> <span class="nf">default_hack</span><span class="p">()</span><span class="w"> </span>-&gt; <span class="nc">Self</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="nb">Default</span>::<span class="n">default</span><span class="p">()</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="cp">#[stable(since = </span><span class="s">&quot;1.4.0&quot;</span><span class="cp">, feature = </span><span class="s">&quot;array_default&quot;</span><span class="cp">)]</span><span class="w"></span>
<span class="k">impl</span><span class="o">&lt;</span><span class="n">T</span><span class="p">,</span><span class="w"> </span><span class="k">const</span><span class="w"> </span><span class="n">N</span>: <span class="kt">usize</span><span class="o">&gt;</span><span class="w"> </span><span class="nb">Default</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">[</span><span class="n">T</span><span class="p">;</span><span class="w"> </span><span class="n">N</span><span class="p">]</span><span class="w"></span>
<span class="k">where</span><span class="w"></span>
<span class="w">    </span><span class="p">[</span><span class="n">T</span><span class="p">;</span><span class="w"> </span><span class="n">N</span><span class="p">]</span>: <span class="nc">ArrayDefault</span><span class="w"></span>
<span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="c1">// ...</span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="203644111"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/good%20example%20for%20const%20generics/near/203644111" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/good.20example.20for.20const.20generics.html#203644111">(Jul 12 2020 at 13:33)</a>:</h4>
<p>I was thinking about something where theres a "broken" implementation that's specialized with a working implementation, but couldn't figure out how to prevent that from leaking out to <code>[NotDefault; 42]</code> and failing at runtime.</p>



<a name="203644958"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/good%20example%20for%20const%20generics/near/203644958" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/good.20example.20for.20const.20generics.html#203644958">(Jul 12 2020 at 13:58)</a>:</h4>
<p><span class="user-mention" data-user-id="116155">@Jake Goulding</span> not completely sure what you mean here. Do you fear that we might somehow end up with calling the <code>DefaultHack</code> default impl?</p>



<a name="203644964"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/good%20example%20for%20const%20generics/near/203644964" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/good.20example.20for.20const.20generics.html#203644964">(Jul 12 2020 at 13:58)</a>:</h4>
<p>We could fix that by using the following <span aria-label="laughing" class="emoji emoji-1f606" role="img" title="laughing">:laughing:</span> (which causes a linker error in case we get this wrong)</p>
<div class="codehilite"><pre><span></span><code><span class="k">impl</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="w"> </span><span class="n">DefaultHack</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">T</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">default</span> <span class="k">fn</span><span class="w"> </span><span class="n">default_hack</span><span class="p">()</span><span class="w"> </span>-&gt; <span class="nc">Self</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="k">extern</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">            </span><span class="k">fn</span> <span class="nf">array_default_specialization_unexpected_default_hack_leak</span><span class="p">()</span><span class="w"> </span>-&gt; <span class="o">!</span><span class="p">;</span><span class="w"></span>
<span class="w">        </span><span class="p">}</span><span class="w"></span>
<span class="w">        </span><span class="k">unsafe</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">            </span><span class="n">array_default_specialization_unexpected_default_hack_leak</span><span class="p">();</span><span class="w"></span>
<span class="w">        </span><span class="p">}</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="203644979"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/good%20example%20for%20const%20generics/near/203644979" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/good.20example.20for.20const.20generics.html#203644979">(Jul 12 2020 at 13:59)</a>:</h4>
<p>I am really confident in my approach, as it shouldn't have any of the subtle problems with trait selection</p>



<a name="203645279"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/good%20example%20for%20const%20generics/near/203645279" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/good.20example.20for.20const.20generics.html#203645279">(Jul 12 2020 at 14:07)</a>:</h4>
<p>My basic unimplemented thoughts were much simpler than your code. Along the lines of</p>
<div class="codehilite"><pre><span></span><code><span class="k">impl</span><span class="w"> </span><span class="nb">Default</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">[</span><span class="n">T</span><span class="p">;</span><span class="w"> </span><span class="n">N</span><span class="p">]</span><span class="w"> </span><span class="p">{}</span><span class="w"></span>
<span class="k">impl</span><span class="w"> </span><span class="nb">Default</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">[</span><span class="n">T</span><span class="p">;</span><span class="w"> </span><span class="mi">0</span><span class="p">]</span><span class="w"> </span><span class="p">{}</span><span class="w"> </span><span class="c1">// specialize on const generic</span>
<span class="k">impl</span><span class="w"> </span><span class="nb">Default</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">[</span><span class="n">T</span><span class="p">;</span><span class="w"> </span><span class="n">N</span><span class="p">]</span><span class="w"> </span><span class="k">where</span><span class="w"> </span><span class="n">T</span>: <span class="nb">Default</span> <span class="p">{}</span><span class="w"> </span><span class="c1">// specialize on trait</span>
</code></pre></div>


<p>But this shape wouldn't prevent calling it for something that shouldn't exist.</p>



<a name="203645344"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/good%20example%20for%20const%20generics/near/203645344" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/good.20example.20for.20const.20generics.html#203645344">(Jul 12 2020 at 14:08)</a>:</h4>
<p>That " // specialize on const generic" is also what I meant by how does specialization interact with const generics.</p>



<a name="203645347"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/good%20example%20for%20const%20generics/near/203645347" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/good.20example.20for.20const.20generics.html#203645347">(Jul 12 2020 at 14:08)</a>:</h4>
<p>Aaah, so that wasn't a concern with my current impl but much rather that you had a similar idea to what I ended up with</p>



<a name="203645361"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/good%20example%20for%20const%20generics/near/203645361" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/good.20example.20for.20const.20generics.html#203645361">(Jul 12 2020 at 14:09)</a>:</h4>
<p><span class="user-mention silent" data-user-id="116155">Jake Goulding</span> <a href="#narrow/stream/122651-general/topic/good.20example.20for.20const.20generics/near/203645344">said</a>:</p>
<blockquote>
<p>That " // specialize on const generic" is also what I meant by how does specialization interact with const generics.</p>
</blockquote>
<p>I am not actually sure how well this already works <span aria-label="thinking" class="emoji emoji-1f914" role="img" title="thinking">:thinking:</span></p>



<a name="203645408"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/good%20example%20for%20const%20generics/near/203645408" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/good.20example.20for.20const.20generics.html#203645408">(Jul 12 2020 at 14:10)</a>:</h4>
<p>might be interesting to try, even if I am kind of afraid of the outcome</p>



<a name="203645585"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/good%20example%20for%20const%20generics/near/203645585" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/good.20example.20for.20const.20generics.html#203645585">(Jul 12 2020 at 14:16)</a>:</h4>
<div class="codehilite"><pre><span></span><code><span class="cp">#![feature(const_generics, min_specialization)]</span><span class="w"></span>

<span class="k">trait</span><span class="w"> </span><span class="n">Example</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">fn</span> <span class="nf">thing</span><span class="p">(</span><span class="o">&amp;</span><span class="bp">self</span><span class="p">);</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">impl</span><span class="o">&lt;</span><span class="k">const</span><span class="w"> </span><span class="n">N</span>: <span class="kt">usize</span><span class="o">&gt;</span><span class="w"> </span><span class="n">Example</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">[</span><span class="kt">u8</span><span class="p">;</span><span class="w"> </span><span class="n">N</span><span class="p">]</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">default</span> <span class="k">fn</span><span class="w"> </span><span class="n">thing</span><span class="p">(</span><span class="o">&amp;</span><span class="bp">self</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="k">for</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="k">in</span><span class="w"> </span><span class="mi">0</span><span class="o">..</span><span class="n">N</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">            </span><span class="n">println</span><span class="o">!</span><span class="p">(</span><span class="s">&quot;default&quot;</span><span class="p">);</span><span class="w"></span>
<span class="w">        </span><span class="p">}</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">impl</span><span class="w"> </span><span class="n">Example</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">[</span><span class="kt">u8</span><span class="p">;</span><span class="w"> </span><span class="mi">3</span><span class="p">]</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">fn</span> <span class="nf">thing</span><span class="p">(</span><span class="o">&amp;</span><span class="bp">self</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="n">println</span><span class="o">!</span><span class="p">(</span><span class="s">&quot;threethreethree&quot;</span><span class="p">);</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>


<p>works as intended!</p>



<a name="203645724"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/good%20example%20for%20const%20generics/near/203645724" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/good.20example.20for.20const.20generics.html#203645724">(Jul 12 2020 at 14:21)</a>:</h4>
<p>is that intended to work or is it working by accident?</p>



<a name="203645783"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/good%20example%20for%20const%20generics/near/203645783" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/good.20example.20for.20const.20generics.html#203645783">(Jul 12 2020 at 14:23)</a>:</h4>
<p>It is intended to work in the future imo, but we haven't spend a lot of effort on getting this to work, so I kind of expected weird errors /ICE here</p>



<a name="203648253"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/good%20example%20for%20const%20generics/near/203648253" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> HeroicKatora <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/good.20example.20for.20const.20generics.html#203648253">(Jul 12 2020 at 15:33)</a>:</h4>
<p>An other thing entirely would be to show const-generics for indexing a slice as an array:<br>
The most amazing thing about this is that generic argument deduction works!</p>
<div class="codehilite"><pre><span></span><code><span class="k">pub</span><span class="w"> </span><span class="k">struct</span> <span class="nc">RangeTo</span><span class="o">&lt;</span><span class="k">const</span><span class="w"> </span><span class="n">N</span>: <span class="kt">usize</span><span class="o">&gt;</span><span class="p">;</span><span class="w"></span>

<span class="k">impl</span><span class="o">&lt;</span><span class="n">T</span><span class="p">,</span><span class="w"> </span><span class="k">const</span><span class="w"> </span><span class="n">N</span>: <span class="kt">usize</span><span class="o">&gt;</span><span class="w"> </span><span class="n">Index</span><span class="o">&lt;</span><span class="n">RangeTo</span><span class="o">&lt;</span><span class="n">N</span><span class="o">&gt;&gt;</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">[</span><span class="n">T</span><span class="p">]</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">type</span> <span class="nc">Output</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[</span><span class="n">T</span><span class="p">;</span><span class="w"> </span><span class="n">N</span><span class="p">];</span><span class="w"></span>
<span class="w">    </span><span class="k">fn</span> <span class="nf">index</span><span class="p">(</span><span class="o">&amp;</span><span class="bp">self</span><span class="p">,</span><span class="w"> </span><span class="n">_</span>: <span class="nc">RangeTo</span><span class="o">&lt;</span><span class="p">{</span><span class="w"> </span><span class="n">N</span><span class="w"> </span><span class="p">}</span><span class="o">&gt;</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="kp">&amp;</span><span class="p">[</span><span class="n">T</span><span class="p">;</span><span class="w"> </span><span class="n">N</span><span class="p">]</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="kd">let</span><span class="w"> </span><span class="n">slice</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">&amp;</span><span class="bp">self</span><span class="p">[</span><span class="o">..</span><span class="n">N</span><span class="p">];</span><span class="w"></span>
<span class="w">        </span><span class="k">unsafe</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="o">&amp;*</span><span class="p">(</span><span class="n">slice</span><span class="p">.</span><span class="n">as_ptr</span><span class="p">()</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="o">*</span><span class="k">const</span><span class="w"> </span><span class="p">[</span><span class="n">T</span><span class="p">;</span><span class="w"> </span><span class="n">N</span><span class="p">])</span><span class="w">  </span><span class="p">}</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">fn</span> <span class="nf">with_beginning</span><span class="p">(</span><span class="n">slice</span>: <span class="kp">&amp;</span><span class="p">[</span><span class="kt">u8</span><span class="p">])</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="p">[</span><span class="n">first</span><span class="p">,</span><span class="w"> </span><span class="n">second</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">slice</span><span class="p">[</span><span class="n">RangeTo</span><span class="p">];</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="203654511"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/good%20example%20for%20const%20generics/near/203654511" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/good.20example.20for.20const.20generics.html#203654511">(Jul 12 2020 at 18:14)</a>:</h4>
<p>my example had deduction as well :P</p>



<a name="203654529"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/good%20example%20for%20const%20generics/near/203654529" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/good.20example.20for.20const.20generics.html#203654529">(Jul 12 2020 at 18:15)</a>:</h4>
<p>it used to not work but that was a bug in how pattern checking was done, not fundamental</p>



<a name="203654547"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/good%20example%20for%20const%20generics/near/203654547" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/good.20example.20for.20const.20generics.html#203654547">(Jul 12 2020 at 18:15)</a>:</h4>
<p>const generics inference works just as well as type inference, because it's implemented the same  :P</p>



<a name="203656207"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/good%20example%20for%20const%20generics/near/203656207" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/good.20example.20for.20const.20generics.html#203656207">(Jul 12 2020 at 19:01)</a>:</h4>
<p><span class="user-mention" data-user-id="229913">@HeroicKatora</span> that <code>unsafe</code> is unsound if <code>slice.len() &lt; N</code> btw</p>



<a name="203656311"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/good%20example%20for%20const%20generics/near/203656311" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/good.20example.20for.20const.20generics.html#203656311">(Jul 12 2020 at 19:03)</a>:</h4>
<p>oh never mind it will panic instead</p>



<a name="203656313"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/good%20example%20for%20const%20generics/near/203656313" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/good.20example.20for.20const.20generics.html#203656313">(Jul 12 2020 at 19:03)</a>:</h4>
<p>carry on</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>